// pages/euqip/equip.js
const {
  API,
  defaultPage,
  equipStatusEnum
} = require('../../utils/const.js');
const {
  getRequest,
  postRequest
} = require('../../utils/request.js');
const re = require('../../utils/request.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showAddForm: false,
    equipList: [],
    selectEquipList: [],
    bgTextStyle: 'dark',
    scrollTop: '200rpx',
    bgColor: '#ff0000',
    bgColorTop: '#00ff00',
    bgColorBottom: '#0000ff',
    nbTitle: '标题',
    nbLoading: false,
    nbFrontColor: '#000000',
    nbBackgroundColor: '#ffffff',
    equipStatusEnum: equipStatusEnum,
    activitiBar: 1,
    searchParams: {},
    pageParams: {
      ...defaultPage,
      size: 20
    },
    current: 1,
    size: 10,
    total: 0,

  },
  oncClearClick: function () {
    this.setData({
      searchParams: {
        ...this.data.searchParams,
        equipStatus: null
      }
    })
  },
  onPickerChange: function (e) {
    const selectIndex = e.detail.value;
    const eqquipStatusList = this.data.equipStatus;
    const selectName = eqquipStatusList[selectIndex].name;
    console.log(selectName);
    this.setData({
      searchParams: {
        ...this.data.searchParams,
        equipStatus: selectName
      }
    })
    this.fetchSelectAbleList();
  },
  selected: function (list, id) {
    console.log('&&&', list, id)
    return list;
  },
  getUserRoles: function () {
    // "clinic:manger"
    // getUser
  },
  equipAddFormSubmit: function (e) {
    console.log(e);
    const formData = e.detail.value;
    postRequest(API.createEquip, formData).then(res => {
      if (res) {
        this.setData({
          showAddForm: false
        })
        this.fetchSelectAbleList();
      }
    })
  },
  onFormRest: function () {
    this.setData({
      showAddForm: false
    })
  },
  onAddClose: function () {
    this.setData({
      showAddForm: false
    })
  },
  onAddClick: function () {
    this.setData({
      showAddForm: true
    })
    // wx.navigateTo({
    //   url: '/pages/equipAddForm/form',
    // })
  },
  onLongTapEquip: function (e) {
    console.log(this.equipStatus)
    const item = e.currentTarget.dataset.value;
    wx.showActionSheet({
      itemList: ['领用', '报废', '丢失'],
      success(res) {
        const {
          tapIndex
        } = res;
        if (tapIndex === 0) {

        }
        if (tapIndex === 1) {

        }
        if (tapIndex === 2) {

        }
        // change
        // console.log(res.tapIndex)
      },
      fail(res) {
        console.log(res.errMsg)
      }
    })
    // this.selectEquipList=[...this.selectEquipList,e.target.dataset.value]
  },
  onReachBottom() {
    console.log("reach")
  },
  fetchAlmostUnEffectList() {
    this.setData({
      equipList: []
    })
    const searchData = {
      ...this.data.searchParams,
      // ...this.data.pageParams
    }
    getRequest(API.almostUnEffect, searchData).then(res => {

      console.log("result", res);
      this.setData({
        equipList: res
      })
    })

  },
  onEquipClick: function (e) {
    console.log(this.selectComponent('.equiplist'))
    var selectItem = e.currentTarget.dataset.value;
    var selectEquipList = this.data.selectEquipList;
    if (selectEquipList.indexOf(selectItem.id) == -1) {
      selectEquipList.push(selectItem.id);
    } else {
      selectEquipList.splice(selectEquipList.indexOf(selectItem.id), 1);
    }
    console.log("selectEquipList", selectEquipList)
    this.setData({
      selectEquipList: [...selectEquipList]
    })
  },
  useEquip() {

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.fetchAllEquipStatus();
  },
  fetchAllEquipStatus() {
    getRequest(API.equipStatus).then(res => {
      if (res && res.length > 0) {
        const resEnum = {}
        res.forEach(element => {
          resEnum[element.name] = element.text
        });
        this.setData({
          equipStatus: res,
          equipStatusEnum: resEnum
        })
      }

    })
  },
  onUploadClick() {
    wx.chooseMedia({
      mediaType: "image",
      count: 1,
      success: (e) => {
        console.log("imageSuccess", e)
        const tempFile = e.tempFiles[0];
        this.setData({
          showAddForm: true
        })
      },
      fail: (e) => {
        this.setData({
          showAddForm: true
        })
      }
    })
  },
  confirmUse(e) {
    var selectedEquip = this.data.selectEquipList;
    console.log("selectedEquip", selectedEquip)
    const that = this;
    wx.showLoading({
      title: '数据正在提交',
    })
    postRequest(API.equipChange, {
      id: selectedEquip,
      type: "USE"
    }).then(res => {
      wx.hideLoading();
      if (res) {
        wx.showToast({
          title: '领用成功',
          icon: 'success',
          duration: 1000,
          success: () => {
            that.setData({
              selectEquipList: []
            })
            this.fetchSelectAbleList();
            // wx.navigateTo({
            //   url: '/pages/useingList/useingList',
            // })
          }
        })
      }
    })
  },
  bindSearchInput(e) {
    var val = e.detail.value;
    console.log(val);
    this.setData({
      searchParams: {
        ...this.searchParams,
        searchStr: val
      }
    })
    this.fetchAlmostUnEffectList();
  },

  searchByKeyWords(keyword) {
    wx.request({
      url: app.globalData.host + "",
    })
  },
  bindEndDateChange(e) {
    // console.log("e",e)
    this.setData({
      effectEnd: e.detail.value
    })
  },
  bindStartDateChange(e) {
    // console.log("e",e)
    this.setData({
      effectStart: e.detail.value
    })
  },
  bindDateChange(e) {
    // console.log("e",e)
    this.setData({
      inStockDate: e.detail.value
    })
  },
  /*****************************/
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.fetchAlmostUnEffectList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    this.setData({
      showAddForm: false
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})